﻿#region Usings

using System;
using System.Globalization;
using System.Windows.Media;

#endregion

namespace SilverBox.Extensions
{
    internal static class ColorExtensions
    {
        // (c) Copyright Microsoft Corporation.  
        // This source is subject to the Microsoft Public License (Ms-PL).  
        // Please see http://go.microsoft.com/fwlink/?LinkID=131993 for details.  
        // All other rights reserved.  
        public static int ToArgb(this Color color)
        {
            var argb = color.A << 24;
            argb += color.R << 16;
            argb += color.G << 8;
            argb += color.B;

            return argb;
        }

        public static Color ParseColor(string value)
        {
            value = value.Replace("#", "");
            var v = Int32.Parse(value, NumberStyles.HexNumber);
            if (value.Length == 6)
                return new Color
                           {
                               A = 255,
                               R = Convert.ToByte((v >> 16) & 255),
                               G = Convert.ToByte((v >> 8) & 255),
                               B = Convert.ToByte((v >> 0) & 255)
                           };
            if (value.Length == 8)
                return new Color
                           {
                               A = Convert.ToByte((v >> 24) & 255),
                               R = Convert.ToByte((v >> 16) & 255),
                               G = Convert.ToByte((v >> 8) & 255),
                               B = Convert.ToByte((v >> 0) & 255)
                           };
            return Colors.Black;
        }
    }
}